package educrypt.gui.components;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import educrypt.gui.view.EditorTextOptionsView;
import educrypt.i18n.Messages;
import educrypt.resources.EduCryptResources;

import javax.swing.JScrollPane;

class EditorTextTypeDialog extends EducryptDialog implements EditorTextOptionsView {

	private static final long serialVersionUID = 8200696352381662957L;

	private final JPanel contentPanel = new JPanel();
	private JTextField txNewTextType;
	private JList lstStoredTextType;
	private JButton btnAddTextType, btnRemoveTextType;
	private JScrollPane scrollPane;

	public EditorTextTypeDialog(Window owner) {
		super(owner);
		setTitle(Messages.getString("guiTextTypeEditor"));

		getContentPane().setLayout(new BorderLayout());
		// contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel lblEditorTextType = new JLabel(
				Messages.getString("guiTextTypeEditor")); //$NON-NLS-1$
		lblEditorTextType.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblEditorTextType.setBounds(10, 11, 378, 24);
		contentPanel.add(lblEditorTextType);

		JLabel lblNewTextType = new JLabel(
				Messages.getString("guiNewTextType")); //$NON-NLS-1$
		lblNewTextType.setBounds(10, 62, 111, 14);
		contentPanel.add(lblNewTextType);

		txNewTextType = new JTextField();
		txNewTextType.setColumns(10);
		txNewTextType.setBounds(131, 57, 266, 24);
		contentPanel.add(txNewTextType);

		ImageIcon iconAdd = new ImageIcon(EduCryptResources.getImageURL("plus24.png"));
		ImageIcon iconRemove = new ImageIcon(EduCryptResources.getImageURL("remove24color.png"));

		btnAddTextType = new JButton(iconAdd);
		btnAddTextType.setBounds(407, 57, 24, 24);
		contentPanel.add(btnAddTextType);

		btnRemoveTextType = new JButton(iconRemove);
		btnRemoveTextType.setBounds(407, 92, 24, 24);
		contentPanel.add(btnRemoveTextType);

		JLabel lblStoredTextType = new JLabel(
				Messages.getString("guiStoredTypes")); //$NON-NLS-1$
		lblStoredTextType.setBounds(10, 92, 111, 28);
		contentPanel.add(lblStoredTextType);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(131, 94, 266, 160);
		contentPanel.add(scrollPane);
		
				lstStoredTextType = new JList();
				scrollPane.setViewportView(lstStoredTextType);
	}

	@Override
	public void addListener(ActionListener ctr) {
		btnAddTextType.setActionCommand(ADD_ITEM);
		btnAddTextType.addActionListener(ctr);

		btnRemoveTextType.setActionCommand(REMOVE_ITEM);
		btnRemoveTextType.addActionListener(ctr);
	}

	@Override
	public String getNewItem() {
		return txNewTextType.getText();
	}

	@Override
	public void showStoredItems(List<String> textTypes) {
		lstStoredTextType.setListData(textTypes.toArray());
	}

	@Override
	public String getSelectedItem() {
		Object selectedValue = lstStoredTextType.getSelectedValue();
		String selectedType = (selectedValue != null) ? selectedValue
				.toString() : "";
		return selectedType;
	}

	@Override
	public void removeNewItem() {
		txNewTextType.setText("");

	}
}
